package com.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.dao.CommentDao;
import com.entity.Comment;

/**
 * @description:评论，以下的降序升序都是通过评论的时间contentTime来的
 * @author:wsc
 * @date:2020年3月8日 下午4:02:47
 */
@Repository
public class CommentDaoImpl implements CommentDao{

	@Resource
	private SessionFactory sessionFactory;
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	@Override
	public boolean addComment(Comment comment) {
		try {
			Serializable serializable = getSession().save(comment);
			if(serializable==null || "".equals(serializable)) return false;
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	@Override
	public void deleteComment(Comment comment) {
		try {
			getSession().delete(comment);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	@Override
	public void updateComment(Comment comment) {
		try {
			getSession().update(comment);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> queryCommentDescByDid(int did) {
		Query<?> query = getSession().createQuery("from Comment where did=:did order by contentTime desc");
		query.setParameter("did", did);
		List<Comment> list = (List<Comment>) query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> queryCommentAscByDid(int did) {
		Query<?> query = getSession().createQuery("from Comment where did=:did order by contentTime asc");
		query.setParameter("did", did);
		List<Comment> list = (List<Comment>) query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> queryCommentPageDescByDid(int did, int pageNum, int pageSize) {
		Query<?> query = getSession().createQuery("from Comment where did=:did order by contentTime desc");
		query.setParameter("did", did);
		query.setFirstResult((pageNum-1) * pageSize);
		query.setMaxResults(pageSize);
		List<Comment> list = (List<Comment>) query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> queryCommentPageAscByDid(int did, int pageNum, int pageSize) {
		Query<?> query = getSession().createQuery("from Comment where did=:did order by contentTime asc");
		query.setParameter("did", did);
		query.setFirstResult((pageNum-1) * pageSize);
		query.setMaxResults(pageSize);
		List<Comment> list = (List<Comment>) query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> queryCommentDescByUid(int uid) {
		Query<?> query = getSession().createQuery("from Comment where uid=:uid order by contentTime desc");
		query.setParameter("uid", uid);
		List<Comment> list = (List<Comment>) query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> queryCommentAscByUid(int uid) {
		Query<?> query = getSession().createQuery("from Comment where uid=:uid order by contentTime asc");
		query.setParameter("uid", uid);
		List<Comment> list = (List<Comment>) query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> queryCommentPageDescByUid(int uid, int pageNum, int pageSize) {
		Query<?> query = getSession().createQuery("from Comment where uid=:uid order by contentTime desc");
		query.setParameter("uid", uid);
		query.setFirstResult((pageNum-1) * pageSize);
		query.setMaxResults(pageSize);
		List<Comment> list = (List<Comment>) query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> queryCommentPageAscByUid(int uid, int pageNum, int pageSize) {
		Query<?> query = getSession().createQuery("from Comment where uid=:uid order by contentTime asc");
		query.setParameter("uid", uid);
		query.setFirstResult((pageNum-1) * pageSize);
		query.setMaxResults(pageSize);
		List<Comment> list = (List<Comment>) query.list();
		return list;
	}

}
